<!-- 混凝土配比管理 -->
<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="show ? rowObj.T1 : rowObj.F1" :xs="show ? rowObj.T2 : rowObj.F2">
        <workSiteLeft ref="workSiteLeft" :show="show" @showLeft="showLeft" @queryVal="queryVal" />
      </el-col>

      <el-col :span="show ? rowObj.T3 : rowObj.F3" :xs="show ? rowObj.T4 : rowObj.F4">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" style="display: flex;justify-content: space-between;">
          <div>
            <el-button style="margin: 3px 0 0 0;" size="mini" :icon="show ? 'el-icon-s-fold' : 'el-icon-s-unfold'" circle @click="showLeft(show)"></el-button>
            <el-form-item label="拌合站" prop="identifier">
              <el-select v-model="queryParams.identifier" clearable placeholder="请选择">
                <el-option v-for="item in stationList" :key="item.name" :label="item.name"
                  :value="item.identifier"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="配比名称" prop="matchingName">
              <el-input v-model="queryParams.matchingName" placeholder="配比名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" v-hasPermi="['zhgd:hntMatchingManage:query']">搜索</el-button>
              <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
            </el-form-item>
          </div>

          <el-form-item>
            <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['zhgd:hntMatchingManage:add']">新增配比</el-button>
          </el-form-item>
        </el-form>

        <!-- <el-row :gutter="10" class="mb8">
          <el-col :span="1.5"><el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['zhgd:hntMatchingManage:add']">新增配比</el-button></el-col>
          <el-col :span="1.5"><el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['zhgd:hntMatchingManage:edit']">修改</el-button></el-col>
          <el-col :span="1.5"><el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['zhgd:hntMatchingManage:remove']">删除</el-button></el-col>
          <el-col :span="1.5"><el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['zhgd:hntMatchingManage:export']">导出</el-button></el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->

        <el-table border v-loading="loading" :data="asphaltSensorCorrectionList"
          @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" align="center" width="60" type="index" />
          <!-- <el-table-column label="配比ID" align="center" prop="id" min-width="120" show-overflow-tooltip/> -->
          <el-table-column label="强度等级" align="center" prop="matchingGrade" />
          <el-table-column label="配比名称" align="center" prop="matchingName" />
          <!-- <el-table-column label="物料用量(kg)" align="center" prop="materialDosage" min-width="120" show-overflow-tooltip /> -->
          <!-- <el-table-column label="物料配比%" align="center" prop="materialMatching" min-width="120" show-overflow-tooltip /> -->
          <!-- <el-table-column label="拌合站名称" align="center" prop="stationName" min-width="120" show-overflow-tooltip/>
          <el-table-column label="是否启用" align="center" prop="status" width="120">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_common_enable" :value="scope.row.status"/>
            </template>
          </el-table-column> -->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetails(scope.row)"
                v-hasPermi="['zhgd:hntMatchingManage:details']">详情</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['zhgd:hntMatchingManage:edit']">修改</el-button>
              <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDisable(scope.row)"
                v-hasPermi="['zhgd:hntMatchingManage:correct']">修正</el-button> -->
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row, scope.$index)"
                v-hasPermi="['zhgd:hntMatchingManage:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 添加或修改混凝土配比管理对话框 -->
    <el-dialog :title="title" :visible.sync="openAll" width="60%" append-to-body :before-close="handleClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px" :disabled="disabledAll">
        <el-row :gutter="20">
          <el-col :span="12"><el-form-item label="强度等级" prop="matchingGrade"><el-input v-model="form.matchingGrade"
                placeholder="强度等级" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="配比名" prop="matchingName"><el-input v-model="form.matchingName"
                placeholder="配比名" /></el-form-item></el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="最低拌合时间" align="center" prop="minimumMixingTime">
              <el-input v-model="form.minimumMixingTime" placeholder="最低拌合时间"><template
                  slot="append">分钟</template></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <!-- <el-col :span="12">
            <el-form-item label="拌合机" prop="identifier">
              <el-select v-model="form.identifier" clearable placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in stationList" :key="item.name" :label="item.name" :value="item.identifier"></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="是否设为默认配比" prop="status" class="querys">
              <el-radio-group v-model="form.status">
                <el-radio label="0">是</el-radio>
                <el-radio label="1">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24"><el-form-item label="备注" prop="remark"><el-input v-model="form.remark" placeholder="备注"
                type="textarea" :rows="2" show-word-limit /></el-form-item></el-col>
        </el-row>

        <el-table border stripe class="table" v-loading="loading" :data="form.zjHntScaleRefList" style="width: 100%">
          <el-table-column label="料仓名称" align="center" prop="materialStoreName">
            <template slot-scope="scope">
              <el-input disabled="disabled" v-model="scope.row.materialStoreName" placeholder="请输入"
                maxlength="40"></el-input>
            </template>
          </el-table-column>

          <el-table-column label="料仓类型" align="center" prop="materialStoreType">
            <template slot-scope="scope">
              <el-select v-model="scope.row.materialStoreType" placeholder="料仓类型" clearable style="width: 100%;"
                disabled="disabled">
                <el-option v-for="dict in dict.type.material_store_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column label="每方物料用量(kg)" align="center" prop="materialDosage">
            <template slot-scope="scope">
              <el-input v-model="scope.row.materialDosage" placeholder="请输入" maxlength="40"></el-input>
            </template>
          </el-table-column>

          <!-- <el-table-column label="物料配比%" align="center" prop="materialMatching">
            <template slot-scope="scope">
              <el-input :disabled="disabled" v-model="scope.row.materialMatching" placeholder="请输入" maxlength="40"></el-input>
            </template>
          </el-table-column> -->

          <!-- <el-table-column label="上限%" align="center" prop="upperLimit">
            <template slot-scope="scope">
              <el-input v-model="scope.row.upperLimit" placeholder="请输入" maxlength="40"></el-input>
            </template>
          </el-table-column> -->

          <!-- <el-table-column label="下限%" align="center" prop="lowerLimit">
            <template slot-scope="scope">
              <el-input v-model="scope.row.lowerLimit" placeholder="请输入" maxlength="40"></el-input>
            </template>
          </el-table-column> -->

          <!-- <el-table-column label="最低拌合时间" align="center" prop="minimumMixingTime">
            <template slot-scope="scope">
              <el-input :disabled="disabled" v-model="scope.row.minimumMixingTime" placeholder="请输入" maxlength="40"></el-input>
            </template>
          </el-table-column> -->
        </el-table>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修正对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body :before-close="handleClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px" :disabled="disabledAll">
        <el-row :gutter="20">
          <el-col :span="12"><el-form-item label="强度等级" prop="matchingGrade"><el-input :disabled="disabled"
                v-model="form.matchingGrade" placeholder="强度等级" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="配比名" prop="matchingName"><el-input :disabled="disabled"
                v-model="form.matchingName" placeholder="配比名" /></el-form-item></el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="最低拌合时间" align="center" prop="minimumMixingTime">
              <el-input :disabled="disabled" v-model="form.minimumMixingTime" placeholder="最低拌合时间"><template
                  slot="append">分钟</template></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否设为默认配比" prop="status" class="querys">
              <el-radio-group v-model="form.status">
                <el-radio label="0">是</el-radio>
                <el-radio label="1">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24"><el-form-item label="备注" prop="remark"><el-input v-model="form.remark" placeholder="备注"
                type="textarea" :rows="2" show-word-limit /></el-form-item></el-col>
        </el-row>

        <el-table border stripe class="table" v-loading="loading" :data="form.zjHntScaleRefList" style="width: 100%">
          <el-table-column label="料仓名称" align="center" prop="materialStoreName">
            <template slot-scope="scope">
              <el-input disabled="disabled" v-model="scope.row.materialStoreName" placeholder="请输入"
                maxlength="40"></el-input>
            </template>
          </el-table-column>

          <el-table-column label="料仓类型" align="center" prop="materialStoreType">
            <template slot-scope="scope">
              <el-select v-model="scope.row.materialStoreType" disabled="disabled" placeholder="料仓类型" clearable
                style="width: 100%;">
                <el-option v-for="dict in dict.type.material_store_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column label="每方物料用量(kg)" align="center" prop="materialDosage">
            <template slot-scope="scope">
              <el-input disabled="disabled" v-model="scope.row.materialDosage" placeholder="请输入"
                maxlength="40"></el-input>
            </template>
          </el-table-column>

          <el-table-column label="上限%" align="center" prop="upperLimit">
            <template slot-scope="scope">
              <el-input
                :disabled="scope.row.materialStoreType == '水泥' || scope.row.materialStoreType == '粉料' ? true : false"
                v-model="scope.row.upperLimit" placeholder="请输入" maxlength="40"></el-input>
            </template>
          </el-table-column>

          <el-table-column label="下限%" align="center" prop="lowerLimit">
            <template slot-scope="scope">
              <el-input
                :disabled="scope.row.materialStoreType == '水泥' || scope.row.materialStoreType == '粉料' ? true : false"
                v-model="scope.row.lowerLimit" placeholder="请输入" maxlength="40"></el-input>
            </template>
          </el-table-column>
        </el-table>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { hntMatchingManageList, hntDetailsList, addHntMatchingManageList, updateHntMatchingManageList, delHntMatchingManageList } from "@/api/zhgd/hntMatchingManage"
import { deptListData } from "@/api/zhgd/hntManage";
import workSiteLeft from "@/views/components/workSiteLeft";
import { hntSiloManageTypeList } from "@/api/zhgd/hntSiloManage";


export default {
  name: "HntMatchingManage",
  dicts: ['sys_yes_no', 'sys_common_status', 'sys_common_enable', 'material_store_type'],
  data() {
    return {
      show: true,
      id: "",
      identifier: "",
      rowObj: {
        T1: 3,
        T2: 24,
        T3: 21,
        T4: 24,
        F1: 0,
        F2: 0,
        F3: 24,
        F4: 24,
      },
      // 标段名称
      deptName: undefined,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 混凝土配比管理表格数据
      asphaltSensorCorrectionList: [],
      // 拌合站搜索框数据
      stationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openAll: false,
      // 是否禁用
      disabledAll: false,
      disabled: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
        zjHntScaleRefList: [],
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        matchingGrade: [{ required: true, message: "强度等级不能为空", trigger: "blur" }],
        matchingName: [{ required: true, message: "配比名不能为空", trigger: "blur" }],
      },
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  components: {
    workSiteLeft,
  },
  created() {
    this.getList();
    this.getProjectList();
  },
  methods: {
    // 左侧树点击事件
    showLeft(show) {
      this.show = !show;
    },
    queryVal(val) {
      // this.identifier = val.id;
      this.queryParams.id = val.id;
      this.id = val.id;
      this.handleQuery();
      this.getProjectList();
    },
    /** 查询混凝土配比管理列表 */
    getList() {
      this.loading = true;
      hntMatchingManageList(this.queryParams).then(response => {
        this.asphaltSensorCorrectionList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 查询拌合站列表 */
    getProjectList() {
      deptListData(this.queryParams).then((response) => {
        this.stationList = response;
      });
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.stationName = data.id;
      this.handleQuery();
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 取消按钮
    cancel() {
      this.getList();
      this.openAll = false;
      this.open = false;
      this.reset();
      this.disabled = false;
      this.form.zjHntScaleRefList = []
    },
    // 点击弹框右上角X号
    handleClose() {
      this.getList();
      this.openAll = false;
      this.open = false;
      this.reset();
      this.disabled = false;
      this.form.zjHntScaleRefList = []
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        stationId: undefined,
        temperature: undefined,
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.workSiteLeft.resetQuery();
      this.queryParams.id = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.id = this.queryParams.id;
      this.identifier = this.queryParams.identifier;
      if (this.id == undefined || this.identifier == undefined) {
        this.$message({ message: '请先选择标段和拌合站', type: 'warning' });
      } else {
        /** 查询料仓类型列表 */
        hntSiloManageTypeList({ id: this.id, identifier: this.identifier }).then((response) => {
          response.data.forEach(item => {
            this.form.zjHntScaleRefList.push({
              materialStoreName: item.materialStoreName,
              materialStoreType: item.materialStoreType,
              stationId:item.stationId,
              materialDosage: null,  //物料用量
              materialMatching: null,   //物料配比
              upperLimit: null,   //上限
              lowerLimit: null,   //下限
              minimumMixingTime: "", //最低拌合时间
            })
          });
        })
        this.openAll = true;
        this.title = "添加混凝土配比管理";
      }
    },
    /** 详情按钮操作 */
    handleDetails(row) {
      // 详情是全部禁用
      this.disabledAll = true;
      const id = row.id || this.ids
      hntDetailsList(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.openAll = true;
        this.title = "修改混凝土配比管理";
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.disabledAll = false;
      const id = row.id || this.ids
      hntDetailsList(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.openAll = true;
        this.title = "修改混凝土配比管理";
      })
    },
    /** 修正按钮操作 */
    handleDisable(row) {
      const id = row.id || this.ids
      hntDetailsList(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改混凝土配比管理";
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.zjHntScaleRefList.forEach((item) => {
            item.materialDosage = Number(item.materialDosage) || 0 //物料用量
            item.materialMatching = Number(item.materialMatching) || 0; //物料配比
            item.upperLimit = Number(item.upperLimit) || 0 //上限
            item.lowerLimit = Number(item.lowerLimit) || 0 //下限
          })
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateHntMatchingManageList(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openAll = false;
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addHntMatchingManageList({ ...this.form, identifier: this.identifier }).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openAll = false;
              this.getList();
              this.form.matchingGrade = '',
                this.form.matchingName = '',
                this.form.remark = '',
                this.form.status = '',
                this.form.zjHntScaleRefList = []
              this.queryParams.identifier = "";
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row, index) {
      const ids = row.id || this.ids;
      this.$modal.confirm(row.id ? `是否确认删除序号为"${index + 1}"的数据项？` : '是否确认删除选中数据项？').then(() => {
        this.loading = true;
        return delHntMatchingManageList(ids);
      })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        }).finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zhgd/asphaltSensorCorrection/export', {
        ...this.queryParams
      }, `asphaltSensorCorrection_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .query {
  .el-form-item__label {
    width: 85px !important;
  }
}

::v-deep .querys {
  .el-form-item__label {
    width: 130px !important;
  }
}

::v-deep .table {
  .el-form-item__content {
    margin-left: 0px !important;
  }
}
</style>
